<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
    <title>AJAX测试</title>
    <style type="text/css">
        .center {
            margin-top: 50px;
            position: relative;
            left: 50%;
            width: 500px;
            text-align: center;
            transform: translateX(-50%);
            border-radius: 20px;
            background-color: white;
            padding: 10px 15px 30px 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row center panel panel-default" id="user-form">
            <h3 style="margin-bottom: 25px;"><b>AJAX测试</b></h3><hr />
            <form class="form-horizontal content">
                <div class="form-group">
                    <label for="type" class="col-xs-3 control-label">Type</label>
                    <div class="col-xs-6">
                        <select class="form-control" id="type">
                            <option value="get">get</option>
                            <option value="post">post</option>
                            <option value="put">put</option>
                            <option value="delete">delete</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="url" class="col-xs-3 control-label">URL</label>
                    <div class="col-xs-6">
                        <input type="text" id="url" class="form-control"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="data" class="col-xs-3 control-label">Data</label>
                    <div class="col-xs-6">
                        <textarea id="data" class="form-control" rows="10"></textarea>
                    </div>
                </div>
                <div class="col-xs-offset-2 col-xs-8 input-group">
                    <button type="submit" class="pull-left col-xs-4 btn btn-success">
                                                                            发送
                    </button>
                    <button type="reset" class="pull-right col-xs-4 btn btn-primary">
                                                                   重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</body>
<script type="text/javascript">
$("form").submit(function() {
    var data = $("#data").val();
    $.ajax({
        type: $("#type").val(),
        url: 'http://localhost:8080/' + $("#url").val(),
        cache: false,
        timeout: 5000,
        data: data === "" ? null : eval('(' + data+ ')'),
        dataType: "json",
        success: function(data) {
            console.log(data);
        }
    });
    return false;
});
</script>
</html>